<template>
  <div class="main">
    <h3>猜你喜欢</h3>
    <div
      class="main-content"
      v-for="(item, index) in mains"
      :key="index"
      @click="cutDetail(item)"
    >
      <img :src="item.imgUrl" />
      <div class="describe">
        <div class="text">
          <p>{{ item.title }}</p>
          <span>{{ item.text }}</span>
        </div>
        <div class="price">
          <p>
            <span>{{ item.discountPrice }}</span
            >元
          </p>
          <span>门市价:￥{{ item.price }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
let mains = ref('')
const store = useStore()
const router = useRouter()
function cutDetail(item) {
  router.push({ name: 'detail', params: { id: item.id } })
  store.commit('homeData/changeDetail', item)
}
function updateCity() {
  // 获取实时数据
  mains.value = store.state.homeData.citys
}
defineExpose({
  updateCity
})
onMounted(() => {
  axios({
    url: 'http://localhost:5000/Home',
    method: 'get'
  }).then((res) => {
    mains.value = res.data.青岛
  })
})
</script>

<style lang="scss" scoped>
.main {
  display: flex;
  flex-direction: column;
  padding: 0 0.1rem;
  h3 {
    width: 100%;
    padding: 0.15rem 0;
    margin: 0;
    border-bottom: 1px solid #ccc;
  }
}
.main-content {
  display: flex;
  padding: 0.08rem 0;
  height: 0.8rem;
  border-bottom: 1px solid #ccc;
  img {
    height: 100%;
    margin-right: 0.1rem;
    width: 1rem;
  }
  .describe {
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: space-between;
    .text {
      height: 50%;
      p {
        font-size: 0.16rem;
        line-height: 0.16rem;
        margin: 0;
      }
      span {
        font-size: 0.1rem;
        margin: 0;
        line-height: 0.1rem;
      }
    }
    .price {
      display: flex;
      width: 100%;
      p {
        margin: 0;
        font-size: 0.1rem;
        color: red;
        margin-right: 0.1rem;
        display: flex;
        align-items: flex-end;
        span {
          font-size: 0.2rem;
          line-height: 0.2rem;
        }
      }
      span {
        margin: 0;
        font-size: 0.1rem;
        display: flex;
        align-items: flex-end;
      }
    }
  }
}
</style>
